jQuery(document).ready(function(){

    jQuery('.stdtablecb .checkall').click(function(){
        var parentTable = jQuery(this).parents('table');
        var ch = parentTable.find('tbody input[type=checkbox]');
        if(jQuery(this).is(':checked')) {
            //check all rows in table
            ch.each(function(){
                jQuery(this).attr('checked',true);
                jQuery(this).parent().addClass('checked');	//used for the custom checkbox style
                jQuery(this).parents('tr').addClass('selected');
            });

            //check both table header and footer
            parentTable.find('.checkall').each(function(){ jQuery(this).attr('checked',true); });

        } else {

            //uncheck all rows in table
            ch.each(function(){
                jQuery(this).attr('checked',false);
                jQuery(this).parent().removeClass('checked');	//used for the custom checkbox style
                jQuery(this).parents('tr').removeClass('selected');
            });

            //uncheck both table header and footer
            parentTable.find('.checkall').each(function(){ jQuery(this).attr('checked',false); });
        }
    });


    jQuery('.stdtablecb tbody input[type=checkbox]').click(function(){
        if(jQuery(this).is(':checked')) {
            jQuery(this).parents('tr').addClass('selected');
        } else {
            jQuery(this).parents('tr').removeClass('selected');
        }
    });



    jQuery('#status').change(function(){
        var values= jQuery('#status').val();
        jQuery('#filter').submit();
    })




    jQuery('.validatebutton').click(function(){
        var sel = false;
        var ispassed= jQuery(this).val();
        var ch = jQuery('#dyntable').find('tbody input[type=checkbox]');		//get each checkbox in a table
        //check if there is/are selected row in table
       // oTable.fnDeleteRow( 0 );
        var rids= '';
        ch.each(function(){
            if(jQuery(this).is(':checked')) {
                sel = true;
                var id= jQuery(this).val();
                rids = rids+id+",";
                jQuery(tableHeaderRows[0]).children().removeClass("checked");
                jQuery(tableHeaderRows[0]).children().children().removeAttr("checked")
            }
        });
       jQuery.ajax({
            url:'../cvcenter/update_approval_cv',
            async:true,
            data: {
                'rids': rids,
                'type':ispassed
            },
            type:'POST',
            success:function(data) {
            console.info(data['is_success']);
            if(data['is_success']==true){
                ch.each(function(){
                    if(jQuery(this).is(':checked')) {
                        var aPos = oTable.fnGetPosition(jQuery(this).parents('tr')[0]);//set to true if there is/are selected row
                        oTable.fnDeleteRow(aPos );
                    }
                })
            }
            jQuery.jGrowl(data['msg'],{closer:false},{corners:'15px'});
           }
        });
          if(!sel) alert('没有选择数据');							//alert to no data selected
    });


    //get data from server and inject it next to row
    jQuery('.stdtable a.toggle').click(function(){

        //show all hidden row and remove all showed data
        jQuery(this).parents('table').find('tr').each(function(){
            jQuery(this).removeClass('hiderow');
            if(jQuery(this).hasClass('togglerow'))
                jQuery(this).remove();
        });

        var parentRow = jQuery(this).parents('tr');
        var numcols = parentRow.find('td').length + 1;
        var url = jQuery(this).attr('href');


        parentRow.after('<tr class="togglerow"><td colspan="'+numcols+'"><div class="toggledata"></div></td></tr>');

        var toggleData = parentRow.next().find('.toggledata');

        parentRow.next().hide();

        //get data from server
        jQuery.post(url,function(data){
            toggleData.append(data);
            parentRow.next().fadeIn();
            parentRow.addClass('hiderow');
        });

        return false;
    });

    jQuery('.toggledata button.cancel, .toggledata button.submit').live('click',function(){
        jQuery(this).parents('.toggledata').animate({height: 0},200, function(){
            jQuery(this).parents('tr').prev().removeClass('hiderow');
            jQuery(this).parents('tr').remove();
        });
        return false;
    });


    var oTable = jQuery('#dyntable').dataTable( {
        "sPaginationType": "full_numbers",
        "bAutoWidth": false,
        "iDisplayLength": 5,
        "bRetrieve": false,
        "bProcessing":false,
        "bServerSide":false,
        "sAjaxSource": null,
        "aLengthMenu": [[5, 25, 50, -1], [5, 25, 50, "全部"]],
        "aoColumns": [
            { "sWidth": "5%" ,"bSortable": false,"sType": "html" },
            { "sWidth": "35%" },
            { "sWidth": "20%" },
            { "sWidth": "10%" },
            { "sWidth": "10%" },
            { "sWidth": "10%" },
            { "sWidth": "10%","bSortable": false,"sType": "html" }
            ],
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "暂时没有需要审批的简历",
            "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
            "sInfoEmpty": "无审批的简历",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "sSearch": "查找:",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "fnCreatedRow": function( nRow, aData, iDataIndex ) {
            var tableRowData = jQuery(nRow).children("td");
            var t=jQuery(tableRowData[0]).children()
            t.wrap('<span class="checkbox"></span>');
            t.click(function(){
                if(jQuery(this).is(':checked')) {
                    t.attr('checked',true);
                    t.parent().addClass('checked');
                } else {
                    t.attr('checked',false);
                    t.parent().removeClass('checked');
                }
            });
            }
         });

     var tableHeaderRows = oTable.find("th");
     jQuery(tableHeaderRows[0]).children().wrap('<span class="checkbox"></span>');
    //for checkbox
    jQuery('input[type=checkbox]').each(function(){
        var t = jQuery(this);
       // t.wrap('<span class="checkbox"></span>');
        t.click(function(){
            if(jQuery(this).is(':checked')) {
                t.attr('checked',true);
                t.parent().addClass('checked');
            } else {
                t.attr('checked',false);
                t.parent().removeClass('checked');
            }
        });

    });

});

